package com.ruoyi.job.task;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.job.domain.YqgBarBills;
import com.ruoyi.job.domain.YqgConsumptionDetails;
import com.ruoyi.job.domain.YqgHands;
import com.ruoyi.job.domain.YqgInformationProject;
import com.ruoyi.job.service.IYqgBarBillsService;
import com.ruoyi.job.service.IYqgConsumptionDetailsService;
import com.ruoyi.job.service.IYqgHandsService;
import com.ruoyi.job.service.IYqgInformationProjectService;
import com.ruoyi.library.api.RemoteLibraryService;
import com.ruoyi.library.api.domain.YhMsgInner;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;
//然后注入


@Component("GuoyefeiTask")
public class GuoyefeiTask
{
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgHandsService yqgHandsService;
    @Autowired
    private IYqgInformationProjectService yqgInformationProjectService;
   /* @Autowired
    private RemoteLibraryService remoteLibraryService;*/
   public List<YqgConsumptionDetails> list(YqgBarBills yqgBarBills)
   {
       yqgBarBills.setHandState("1");
       YqgConsumptionDetails yqgConsumptionDetails=new YqgConsumptionDetails();
       List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
       if(list.size()==0){
           return  new ArrayList<YqgConsumptionDetails>();
       }
       YqgHands hand=new YqgHands();
       hand.setHandBrand(yqgBarBills.getHandBrand());
       hand.setLockedState("0");
       List<YqgHands> hlist=yqgHandsService.selectYqgHandsList(hand);
       //并牌没解锁
       if(list.get(0).getMergeStatus().equals("0")&&hlist.size()!=0){
           return  new ArrayList<YqgConsumptionDetails>();
       }
       //并牌已经解锁的
       else if(list.get(0).getMergeStatus().equals("0")&&hlist.size()==0){
           yqgConsumptionDetails.setBarBillsId(list.get(0).getID());
           yqgConsumptionDetails.setHandState("1");
           yqgConsumptionDetails.setConsumptionStatus("0");
           List<YqgConsumptionDetails> ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
           //过滤并牌
           List<YqgConsumptionDetails> bplist=new ArrayList<>();
           for(int i=0;i<ylist.size();i++){

               if(ylist.get(i).getMergeBarBillsId()!=null){
                   bplist.add(ylist.get(i));
               }
           }
           ylist.removeAll(bplist);
           //显示转化
           for(int i=0;i<ylist.size();i++){
               if(ylist.get(i).getBeforeBarBillsId()!=null){
                   ylist.get(i).setNote10("来自"+ylist.get(i).getBeforeLeave()+"的留牌");
               }
           }
           //过滤过夜费
           List<YqgConsumptionDetails> gylist=new ArrayList<>();
           for(int i=0;i<ylist.size();i++){
               if(ylist.get(i).getEntryName().equals("过夜费")){
                   Date retime= ylist.get(i).getRecordingTime();
                   LocalDateTime Record= LocalDateTime.ofInstant(retime.toInstant(), ZoneId.systemDefault());
                   String date=DateUtils.dateTimeNow("yyyy-MM-dd");
                   String start=date+" 00:00:00";
                   String end=date+" 06:00:00";
                   Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
                   Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
                   LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
                   LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());
                   if(Record.isAfter(startTime)&&Record.isBefore(endTime)){
                       LocalDateTime newDateTime = Record.plusHours(2);
                       LocalDateTime currentDateTime = LocalDateTime.now();
                       if(currentDateTime.isBefore(newDateTime)){
                           gylist.add(ylist.get(i));
                       }

                   }

               }
           }
           ylist.removeAll(gylist);
           return ylist;
       }
       //没有并牌的
       else{
           yqgConsumptionDetails.setBarBillsId(list.get(0).getID());
           yqgConsumptionDetails.setHandState("1");
           yqgConsumptionDetails.setConsumptionStatus("0");

           List<YqgConsumptionDetails> ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
           for(int i=0;i<ylist.size();i++){
               if(ylist.get(i).getBeforeBarBillsId()!=null){
                   ylist.get(i).setNote10("来自"+ylist.get(i).getBeforeLeave()+"的留牌");
               }
           }
           //并牌查询
           yqgConsumptionDetails.setBarBillsId(null);
           yqgConsumptionDetails.setMergeBarBillsId(list.get(0).getID());
           List<YqgConsumptionDetails> blist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
           List<YqgConsumptionDetails> removelist=new ArrayList<>();
           for(int i=0;i< blist.size();i++){

               if(blist.get(i).getMergeBarBillsId().equals(list.get(0).getID())&&blist.get(i).getBarBillsId().equals(list.get(0).getID())){
                   removelist.add(blist.get(i));
               }
           }
           if(removelist.size()!=0){
               blist.removeAll(removelist);
           }
           for(int i=0;i<blist.size();i++){
               if(blist.get(i).getMergeBarBillsId()!=null){
                   blist.get(i).setNote10("来自"+blist.get(i).getHandBrand()+"的并牌");
               }
           }
           if(blist.size()==0){
               //过滤过夜费
               List<YqgConsumptionDetails> gylist=new ArrayList<>();
               for(int i=0;i<ylist.size();i++){
                   if(ylist.get(i).getEntryName().equals("过夜费")){
                       Date retime= ylist.get(i).getRecordingTime();
                       LocalDateTime Record= LocalDateTime.ofInstant(retime.toInstant(), ZoneId.systemDefault());
                       String date=DateUtils.dateTimeNow("yyyy-MM-dd");
                       String start=date+" 00:00:00";
                       String end=date+" 06:00:00";

                       Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
                       Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
                       LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
                       LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());
                       if(Record.isAfter(startTime)&&Record.isBefore(endTime)){
                           LocalDateTime newDateTime = Record.plusHours(2);
                           LocalDateTime currentDateTime = LocalDateTime.now();
                           if(currentDateTime.isBefore(newDateTime)){
                               gylist.add(ylist.get(i));
                           }

                       }

                   }
               }
               ylist.removeAll(gylist);
               return ylist;
           }
           else{
               //增加并牌数据
               ylist.addAll(blist);
               //过滤过夜费
               List<YqgConsumptionDetails> gylist=new ArrayList<>();
               for(int i=0;i<ylist.size();i++){
                   if(ylist.get(i).getEntryName().equals("过夜费")){
                       Date retime= ylist.get(i).getRecordingTime();
                       LocalDateTime Record= LocalDateTime.ofInstant(retime.toInstant(), ZoneId.systemDefault());
                       String date=DateUtils.dateTimeNow("yyyy-MM-dd");
                       String start=date+" 00:00:00";
                       String end=date+" 06:00:00";
                       Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
                       Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
                       LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
                       LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());
                       if(Record.isAfter(startTime)&&Record.isBefore(endTime)){
                           LocalDateTime newDateTime = Record.plusHours(2);
                           LocalDateTime currentDateTime = LocalDateTime.now();
                           if(currentDateTime.isBefore(newDateTime)){
                               gylist.add(ylist.get(i));
                           }

                       }

                   }
               }
               ylist.removeAll(gylist);
               return ylist;
           }

       }

   }

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }
    //0点生成过夜费
    public void ryNoParams()
    {

        YqgBarBills yqgBarBills=new YqgBarBills();
        yqgBarBills.setHandState("1");
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        //先查在台的手牌
        for(int i=0;i<list.size();i++){
            //剔除儿童牌与外卖牌
            if(!list.get(i).getHandCategory().equals("2")&&!list.get(i).getHandCategory().equals("3")){
                int flag=0;//这里判断开房标志
                System.out.println("过夜费"+list.get(i).getHandBrand());
                List<YqgConsumptionDetails> conlist=list(list.get(i));

                System.out.println("消费"+conlist.size());
                if(conlist.size()!=0){
                    for(int j=0;j<conlist.size();j++){
                        if(conlist.get(j).getProjectCategory().equals("00")){
                            flag=1;
                        }
                    }
                }
                if(flag==0){
                    /*LoginUser loginUser = getLoginUser();
                    SysUser sysUser = loginUser.getSysUser();*/

                    YqgConsumptionDetails yqgConsumptionDetails=new YqgConsumptionDetails();
                    //服务员信息
                   /* */
                    yqgConsumptionDetails.setUserId(1L);
                    yqgConsumptionDetails.setDeptId(103L);
                    yqgConsumptionDetails.setNickName("系统管理员");
                    yqgConsumptionDetails.setUserName("admin");
                    yqgConsumptionDetails.setPost("管理员");
                    yqgConsumptionDetails.setDepartment("3");
                    yqgConsumptionDetails.setRetentionStatus("1");
                    yqgConsumptionDetails.setMergeStatus("1");
                    yqgConsumptionDetails.setHandState("1");
                    yqgConsumptionDetails.setOpeningTime(DateUtils.getNowDate());
                    //手牌信息
                    yqgConsumptionDetails.setHandBrand(list.get(i).getHandBrand());
                    yqgConsumptionDetails.setHandCategory(list.get(i).getHandCategory());
                    yqgConsumptionDetails.setHandState(list.get(i).getHandState());
                    yqgConsumptionDetails.setMergeStatus(list.get(i).getMergeStatus());
                    yqgConsumptionDetails.setRetentionStatus(list.get(i).getRetentionStatus());
                    yqgConsumptionDetails.setOriginalHand(list.get(i).getOriginalHand());
                    yqgConsumptionDetails.setTogether(list.get(i).getTogether());
                    yqgConsumptionDetails.setBarBillsId(list.get(i).getID());
                    yqgConsumptionDetails.setHandoverStatus("0");
                    YqgInformationProject gyproject=new YqgInformationProject();
                    gyproject.setProjectCode("00008");
                    List<YqgInformationProject> gylist = yqgInformationProjectService.selectYqgInformationProjectList(gyproject);
                    gyproject=gylist.get(0);
                    yqgConsumptionDetails.setProjectCode(gyproject.getProjectCode());
                    yqgConsumptionDetails.setProjectCategory(gyproject.getProjectCategory());
                    yqgConsumptionDetails.setEntryName(gyproject.getEntryName());
                    yqgConsumptionDetails.setMnemonicCode(gyproject.getMnemonicCode());
                    yqgConsumptionDetails.setUnit(gyproject.getUnit());
                    yqgConsumptionDetails.setUnitPrice(gyproject.getUnitPrice());
                    yqgConsumptionDetails.setCommissionPrice(gyproject.getCommissionPrice());
                    yqgConsumptionDetails.setQuantity("1");
                    yqgConsumptionDetails.setConsumptionStatus("0");
                    yqgConsumptionDetails.setTotalAmount(gyproject.getUnitPrice());
                    yqgConsumptionDetails.setRecordingTime(DateUtils.getNowDate());

                    yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails);

                }
            }
        }

    }
}
//定时器
/*
@Component
public class SendMsgTask {
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    YqgBarBills yqgBarBills=new YqgBarBills();
    @Scheduled(fixedRate = 60 * 60 * 1000) // 每十分钟运行一次
    public void sendMsg() {
        System.out.println("进来了");

    }

}*/
